﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SharingCenter.Web.Classes;
using SharingCenter.Web.Classes.Account;
using System.Web.Security;
using SharingCenter.Web.Classes.Profiles;
using SharingCenter.Web.Classes.Message;
using SharingCenter.Web.Classes.Contact;

namespace SharingCenter.Web.Account
{
    public partial class Activate : PageBase
    {
        string email, vcode, nickName;
        bool hasError = false;
        Guid userId;

        protected void Page_Init(object sender, EventArgs e)
        {

            if (Request.QueryString["email"] == null || Request.QueryString["c"] == null)
            {
                hasError = true;
            }
            else
            {
                email = Request.QueryString["email"];
                vcode = Request.QueryString["c"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!hasError)
            {
                MembershipUser u = Membership.GetUser(email);
                aspnet_User user = Classes.Account.AccountOperations.GetUser(email);
                userId = user.UserId;
                if(CreateProfile(ref u))
                {
                    AddContact(email, ref u, userId);
                    AccountActivateResult r = AccountOperations.ActivateUser(email, vcode, out u);
                    ProcessActivateResult(r, ref u);
                }
            }
            else
            {
                MultiView1.ActiveViewIndex = hasError ? 1 : 0;
            }
        }

        private void AddContact(string email, ref MembershipUser u, Guid userId)
        {
            string[] args = u.Comment.Split(WebConsts.STR_SEPERATOR);
            try
            {
                if (args[3] != null)
                {
                    try
                    {
                        Guid refUserId = new Guid(args[3]);
                        ContactOperations.ApproveContact(refUserId, userId, "", true, true);
                    }
                    catch { }
                }
            }
            catch { }
        }

        void ProcessActivateResult(AccountActivateResult r, ref MembershipUser u)
        {
            switch (r)
            {
                case AccountActivateResult.Success:
                    SendActivateMail(ref u);
                    MultiView1.ActiveViewIndex = 0;
                    break;
                case AccountActivateResult.Fail:
                    MultiView1.ActiveViewIndex = 1;
                    break;
                case AccountActivateResult.AlreadyActivated:
                    MultiView1.ActiveViewIndex = 2;
                    break;
                case AccountActivateResult.InvalidCode:
                    MultiView1.ActiveViewIndex = 2;
                    break;
                case AccountActivateResult.NotFound:
                    MultiView1.ActiveViewIndex = 1;
                    break;
            }
        }

        private void SendActivateMail(ref MembershipUser u)
        {
            Dictionary<string, string> replacement = new Dictionary<string, string>();
            replacement.Add("UserName", email);
            replacement.Add("Email", email);
            replacement.Add("NickName", nickName);
            MailOperations.SendSystemMail("Account", "UserActivated", email, replacement);
        }

        bool CreateProfile(ref MembershipUser u)
        {
            string[] args = u.Comment.Split(WebConsts.STR_SEPERATOR);
            if(args.Length!=3)
            {
                args[2] = "匿名";
                args[1] = "NM";
            }
            nickName = args[2];
            return ProfileOperations.InitializeProfile(u.UserName, args[2], args[1]);
        }
    }
}
